package vip.huhailong.redismq.service.impl;


import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import vip.huhailong.redismq.service.ISegmentService;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName: ServiceFactory
 * @author:
 * @date:
 * @Description: 引擎service实例工厂加载类
 */
@Component
public class ServiceFactory implements ApplicationContextAware {

    private static Map<String, ISegmentService> SERVICE_MAP;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        SERVICE_MAP = new HashMap<>();
        Map<String, ISegmentService> beanMap = applicationContext.getBeansOfType(ISegmentService.class);
        if(beanMap != null){
            beanMap.forEach((key,value)->SERVICE_MAP.put(value.getType(),value));
        }
    }

    public static <T extends ISegmentService> T getService(String type){
        return (T) SERVICE_MAP.get(type);
    }
}
